package cn.sc.allinpay.allfavour.business.coupon;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;

import java.math.BigDecimal;

import cn.pai.common.utils.AmtUtils;
import cn.pai.mvp.intervenor.Intervenor;
import cn.pai.mvp.presenter.PaiPresenter;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.common.data.CouponsData;
import cn.sc.allinpay.allfavour.business.common.data.MemberData;
import cn.sc.allinpay.allfavour.business.common.data.OrderData;

/**
 * 券核销 presenter
 * author：pany
 * on 2017/12/1 11:27
 */
public class CouponPresenter extends PaiPresenter<CouponContract.View> implements CouponContract.Presenter {

    private CouponModel model = new CouponModel();

    private OrderData payOrder;//支付订单
    private BigDecimal transAmt;//交易金额
    private MemberData member;//会员信息
    private BigDecimal balAmt;//剩余金额 减去会员权益折扣和电子钱包折扣后的金额

    private BigDecimal discountAmt;//优惠金额
    private BigDecimal paidAmt;//剩余金额

    @Override
    protected Intervenor createIntervenor() {
        return new Intervenor() {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Bundle bundle = view.getBundle();
                payOrder = bundle.getParcelable(Constant.BundleKey.PAY_ORDER);
            }
        };
    }

    @Override
    public void start() {
        if (initData()) {
            initView();
        } else {
            view.finish();
        }

    }

    private boolean initData() {
        if (payOrder == null) {
            view.toast("订单数据不能为空");
            return false;
        }
        if (TextUtils.isEmpty(payOrder.getTransAmt())) {
            view.toast("交易金额不能为空");
            return false;
        }
        if (payOrder.getMemberData() == null) {
            view.toast("会员信息不能为空");
            return false;
        }
        if (payOrder.getMemberData().getCoupons() == null) {
            view.toast("会员卡券信息不能为空");
            return false;
        }
        transAmt = new BigDecimal(payOrder.getTransAmt());//交易金额
        member = payOrder.getMemberData();//会员数据

        balAmt = new BigDecimal(payOrder.getTransAmt());//剩余金额 减去会员权益折扣和电子钱包折扣后的金额
        if (payOrder.getMemberData().getOilDiscountAmt() != null) {//会员权益优惠
            balAmt = balAmt.subtract(payOrder.getMemberData().getOilDiscountAmt());
        }
        if (!TextUtils.isEmpty(payOrder.getDisEWalletAmt())) {//电子钱包优惠
            balAmt = balAmt.subtract(new BigDecimal(payOrder.getDisEWalletAmt()));
        }
        return true;
    }

    private void initView() {
        view.couponsAdapterChange(member.getCoupons());
        calculateAmt();
    }

    @Override
    public void calculateAmt() {
        discountAmt = BigDecimal.ZERO;
        for (int i = 0; i < member.getCoupons().size(); i++) {
            CouponsData data = member.getCoupons().get(i);
            if (data.isUse()) {//选中才计算
                if (data.getType() == 0) {//0金额 1抵扣 2计次
                    discountAmt = discountAmt.add(new BigDecimal(data.getUseAmt()));
                } else {
                    discountAmt = discountAmt.add(new BigDecimal(AmtUtils.changeF2Y(data.getDiscountedAmt())).
                            multiply(new BigDecimal(data.getUseNumber())));
                }
            }
        }
        paidAmt = balAmt.subtract(discountAmt);
        if (BigDecimal.ZERO.compareTo(paidAmt) > 0) {//剩余金额为负数，直接至为0
            paidAmt = BigDecimal.ZERO;
        }
        view.showPaidAmt(AmtUtils.formatY(paidAmt));

        view.saveOrderPaidAmt(paidAmt);
    }

    @Override
    public void setForResult() {
        if (discountAmt == null) {
            view.prompt("优惠金额不能为空");
            return;
        }
        if (paidAmt == null) {
            view.prompt("剩余金额不能为空");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList(Constant.BundleKey.COUPONS, member.getCoupons());//卡券列表
        bundle.putString(Constant.BundleKey.DISCOUNT_AMT, (BigDecimal.ZERO.compareTo(discountAmt) != 0 ? discountAmt.toString() : null));//优惠金额
        bundle.putString(Constant.BundleKey.PAID_AMT, paidAmt.toString());//剩余金额
        view.setResult(Activity.RESULT_OK, bundle);
        view.finish();
    }
}
